package com.ashlikun.okhttputils.simple.retrofit

import android.util.Log
import com.ashlikun.okhttputils.simple.MyApp
import com.ashlikun.okhttputils.simple.data.WeiJinModel
import kotlinx.coroutines.*
import java.io.BufferedWriter
import java.io.Closeable
import java.io.File
import java.io.FileWriter
import java.io.IOException
import java.nio.charset.Charset

/**
 * 作者　　: 李坤
 * 创建时间: 2021/4/9　17:23
 * 邮箱　　：496546144@qq.com
 *
 * 功能介绍：
 */
class RetrofitTest {
    companion object {
        fun getAddd() = 22222 / 11

        @JvmStatic
        fun start() {
            val aa = 11111;
            Log.e("qqq aaa sss", (aa is Number).toString())
//            val aa = ApiOther.api.testx(1)
//            Log.e("aaaaaaaa", "$aa")
            GlobalScope.launch(CoroutineExceptionHandler { coroutineContext, throwable ->
                throwable.printStackTrace()
            }) {
                GlobalScope.async {
                    delay(2000)
                    Log.e("ggggggggg", "2")
                    delay(2000)
                }
                runCatching {
                    withContext(Dispatchers.Main) {
//                       var aaa2 = ApiOther.api.test(111, WeiJinModel())
//                       Log.e("bbb", aaa2?.dataX?.toString())
//                       Log.e("bbb2", aaa2?.newData?.toString())
                        val file = File(MyApp.app.cacheDir, "123.png")
                        writeString(file, "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
//                        ApiOther.api.testFile(file, 1, "ddd")
                        /**
                         * api	SYNO.FileStation.Upload
                         * create_parents	true
                         * method	upload
                         * mtime	1729213895000
                         * overwrite	true
                         * path	/home/Photos/MobileBackup/一加 11
                         * size	525066
                         * version	2
                         */
//                        ApiOther.api.fileUpload(file, "/home/Photos/MobileBackup/一加 11", 1729213895000L, 525066)

                        runCatching {
                            val response = ApiOther.api.downloadFile("/home/Photos/MobileBackup/一加 11/上传标记.txt")
                            Log.e("wwwww", "上传标记 ${response.toHexStr}")
//                            if (response.isSuccessful) {
//                                val responseBody = response.body
//                                // 读取响应体并使用正确的字符集解码
//                                val responseString = responseBody?.string()?.let { it.toByteArray(Charset.forName("UTF-8")).toString(Charset.forName("UTF-8")) }
//                                Log.e("wwwww", "上传标记 ${responseString}")
//                            }
                        }.onFailure { it.printStackTrace() }
                    }
                }.onFailure {
                    it.printStackTrace()
                    Log.e("sssssss22222222ss", "")
                }
            }
//            GlobalScope.launch {
//                var aaa = ApiOther.api.testx(111)
//                Log.e("aaaa", aaa?.toString() + "")
////                aaa = ApiOther.api.test( 11111, WeiJinModel())
////                Log.e("bbb", aaa?.json + "")
////                var aaa = ApiOther.api.test7(111, "asdasdsad")
//                runCatching {
//                    var aaa2 = ApiOther.api.test(111, WeiJinModel())
//                    Log.e("bbb", aaa2?.json + "")
//                }.onFailure {
//                    Log.e("sssssssss", "")
//                }
//
//            }

        }

        fun createOrExistsDir(file: File): Boolean {
            return file != null && if (file.exists()) file.isDirectory else file.mkdirs()
        }

        fun createOrExistsFile(file: File): Boolean {
            if (file.exists()) {
                return file.isFile
            }
            return if (!createOrExistsDir(file.parentFile)) false
            else try {
                file.createNewFile()
            } catch (e: IOException) {
                e.printStackTrace()
                false
            }
        }

        fun writeString(
            file: File, content: String, append: Boolean = false
        ): Boolean {
            if (!createOrExistsFile(file)) {
                return false
            }
            var bw: BufferedWriter? = null
            return try {
                bw = BufferedWriter(FileWriter(file, append))
                bw.write(content)
                true
            } catch (e: IOException) {
                e.printStackTrace()
                false
            } finally {
                close(bw)
            }
        }

        /**
         * 关闭流
         */
        fun close(io: Closeable?): Boolean {
            try {
                io?.close()
            } catch (e: IOException) {
            }
            return true
        }
    }


}

/**
 * ByteArray -> 16进制字符串
 */
inline val ByteArray.toHexStr
    get() = bytesToHexString(this)

fun bytesToHexString(src: ByteArray?, joint: String? = null): String {
    if (src == null || src.isEmpty()) {
        return ""
    }
    val b = StringBuilder()
    for (element in src) {
        //String.format 性能低下
//            b.append(String.format("%02X", src[i] and 0xFF.toByte()))
        var hex = Integer.toHexString(element.toInt() and 0xFF)
        if (hex.length == 1) {
            b.append("0")
        }
        b.append(hex)
        if (joint != null) {
            b.append(joint)
        }
    }
    return b.toString()
}